# -*- coding: utf-8 -*-
import os
import sys
import aircv as ac
import time

from common.auto_adb import auto_adb
from example.test_find_png import findOf

bashPath = os.path.realpath(os.curdir) + "\\pic\\"
sys.path.append(r"D:\Tools\adb1")

# os.popen("adb shell am start -n com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin")

hight, width = 2000, 1000

adb = auto_adb()


def doMatch(source, target):
    # imsrc = ac.imread(source)
    # imobj = ac.imread(target)
    # matchResult = ac.find_template(imsrc, imobj, 0.5)
    point = findOf(source, target)
    if not point:
        return None
    return {"result": [point[0], point[1]]}


def clickXY(x, y):
    adb.run("shell input tap " + str(x) + " " + str(y))


def screenShot():
    adb.run("shell screencap -p /sdcard/t3.png")
    time.sleep(0.4)
    adb.run("pull /sdcard/t3.png " + bashPath + "screen.png")


def swipe(ox, oy, tx, ty):
    adb.run("shell input swipe " + str(ox) +
            " " + str(oy) + " " + str(tx) + " " + str(ty))


def checkPic(path):
    screenShot()
    matchResult = doMatch(bashPath + "screen.png", bashPath + path)
    if matchResult:
        return True
    return False


def clickPic(path, x=0, y=0):
    screenShot()
    matchResult = doMatch(bashPath + "screen.png", bashPath + path)
    if matchResult:
        clickXY(matchResult['result'][0] + float(x),
                matchResult['result'][1] + float(y))
        return True
    return False


def clickPicArray(paths, x=0, y=0):
    screenShot()
    for p in paths:
        matchResult = doMatch(bashPath + "screen.png", bashPath + p)
        if matchResult:
            rx = matchResult['result'][0]
            ry = matchResult['result'][1]
            print("match:%s, click{x:%s,y:%s}" % (p, rx, ry))
            clickXY(rx + float(x),
                    ry + float(y))
            return True
    return False


def swipe1_8():
    ox = width / 2
    tx = ox
    oy = hight / 2
    ty = hight / 8
    swipe(ox, oy, tx, ty)


# swipe1_4()
# clickPic("alipay_help.png")


def mysl():
    # os.system("adb connect 10.128.107.129")
    # 检测锁屏

    while True:
        print("打开蚂蚁森林 .")
        adb.run(
            "shell am start -n com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.AlipayLogin")
        flag = clickPic('2.png')
        if not flag:
            time.sleep(2)
        else:
            break
    # 自己能量收集
    while True:
        flag1 = clickPicArray(["g.png"])
        if not flag1:
            time.sleep(1)
            break

    # 滑动到更多好友
    flag = False
    while not flag:
        print("打开好友列表 .")
        swipe1_8()
        time.sleep(1)
        if (checkPic("4.png")):
            flag = clickPic("4.png")
    flag = False
    while not flag:
        print("循环收取 .")
        if (checkPic("9.png")):
            while True:
                flag1 = clickPicArray(["7.png", "g.png", "8.png", "81.png"], y=-10)
                if not flag1:
                    adb.run("shell input keyevent BACK")
                    time.sleep(1)
                    break
        if (checkPic("5.png")):
            time.sleep(1)
            clickPic("5.png")
        elif (checkPic("6.png")):
            time.sleep(1)
            clickPic("6.png")
        else:
            swipe1_8()
            time.sleep(1)
            continue

        if checkPic("10.png"):
            break


mysl()
